
clear all
cap log close

log using "${logfiles}step08.log", replace

set seed 1234556 
* find firm matches 
forvalues Y=2006 (1) 2013 {

use "$dataout\treatment_firms.dta", clear
keep sykstun year_event year wp_crime  manager_sub  manager_sub2 mm mf incgap new_wp_crime couple

keep if year_event==`Y'


local minus1=`Y'-1 
local minus2=`Y'-2 
local minus3=`Y'-3 
local minus4=`Y'-4 
local minus5=`Y'-5 

        * merge with past 5 years firm level characteristics to do nn matching 
		merge m:1 sykstun using "${dataout}plant_char_`Y'", keepusing (employee female_employee switch newhire earnings nace switch_female)
		drop _merge
		
		
		rename earnings earnings_0
		rename employee employee_0
		rename female_employee female_employee_0
		rename switch switch_0
		rename newhire newhire_0
		rename nace nace_0 
		gen share_female_0=female_employee_0/employee_0
		
		
		merge m:1 sykstun using "${dataout}plant_char_`minus1'" , keepusing (employee female_employee switch newhire earnings nace switch_female)
		drop if _merge==2
		rename _merge _merge1
		
		rename earnings earnings_1
		rename employee employee_1
		rename female_employee female_employee_1
		rename switch switch_1
		rename newhire newhire_1
		rename nace nace_1
		rename switch_female switch_female_1
		gen share_female_1=female_employee_1/employee_1
		
		merge m:1 sykstun using "${dataout}plant_char_`minus2'" , keepusing (employee female_employee switch newhire earnings nace switch_female )
		drop if _merge==2
		rename _merge _merge2
		
		rename earnings earnings_2
		rename employee employee_2
		rename female_employee female_employee_2
		rename switch switch_2
		rename newhire newhire_2
		rename nace nace_2
		rename switch_female switch_female_2
		gen share_female_2=female_employee_2/employee_2
		
		merge m:1 sykstun using "${dataout}plant_char_`minus3'" , keepusing (employee female_employee switch newhire earnings nace switch_female )
		drop if _merge==2
		rename _merge _merge3
		
	
		
		merge m:1 sykstun using "${dataout}plant_char_`minus4'" , keepusing (employee female_employee switch newhire earnings nace switch_female )
		drop if _merge==2
		rename _merge _merge4
	
		
		merge m:1 sykstun using "${dataout}plant_char_`minus5'" , keepusing (employee female_employee switch newhire earnings nace switch_female )
		drop if _merge==2
		rename _merge _merge5
	
		
		
		gen share_newhire_1=newhire_1/employee_0
		gen share_newhire_2=newhire_2/employee_0
	
	
		gen share_switch_1=switch_1/employee_0
		gen share_switch_2=switch_2/employee_0
	
		gen share_switch_female_1=switch_female_1/employee_0
		gen share_switch_female_2=switch_female_2/employee_0
	
		
		
		*** generate firm life length based on merge ids 
		gen firm_life=1 if  _merge1==3 
		replace firm_life=2 if  _merge2==3 
		replace firm_life=3 if  _merge3==3 
		replace firm_life=4 if  _merge4==3 
		replace firm_life=5 if _merge5==3 
		
		drop if firm_life==5 
		
		ereplace year=max(year)
		

	replace wp_crime=0 if wp_crime==.
	drop if _n>100000 & wp_crime==0

	

	gen log_ave_earnings_1=log(earnings_1+1)
	gen log_ave_earnings_2=log(earnings_2+1)

	
	gen log_employee_1=log(employee_1)
	gen log_employee_2=log(employee_2)

	
	gen outcomes=1
	
	replace  log_employee_2=0 if  log_employee_2==. 
	replace log_ave_earnings_2=0 if log_ave_earnings_2==. 
	replace share_female_2=0 if share_female_2==. 
	replace share_newhire_2=0 if share_newhire_2==. 
	replace nace_2=0 if nace_2==. 
	replace share_switch_2=0 if share_switch_2==. 
	
	
		replace  log_employee_1=0 if  log_employee_1==. 
	replace log_ave_earnings_1=0 if log_ave_earnings_1==. 
	replace share_female_1=0 if share_female_1==. 
	replace share_newhire_1=0 if share_newhire_1==. 
	replace nace_1=0 if nace_1==. 
	replace share_switch_1=0 if share_switch_1==. 
	
	
    sort sykstun wp_crime 
	
	capture noisily teffects nnmatch (outcomes log_employee_* log_ave_earnings* share_female_* share_newhire_* nace_* share_switch_* firm_life) (wp_crime),  osample (new)
	
	* drop obs that violate overlap assumptions
	drop if new==1

	teffects nnmatch (outcomes log_employee_* log_ave_earnings* share_female_* share_newhire_* nace_* share_switch_* firm_life)  (wp_crime),  gen (match_id)
   
	* find their matches and matched ids
	save "$dataout\complete", replace 
	gen rownum=_n
	
	preserve 
	keep if wp_crime==1 & !missing(match_id1)
	save "$dataout\treatment", replace
	restore 
	
	keep if wp_crime==0 & !missing(match_id1)
	drop match_id1
	rename rownum match_id1
	
	save "$dataout\control", replace
	
	merge 1:m match_id1 using "$dataout\treatment", keepusing (match_id1)
	keep if _merge==3
	drop _merge
	tab match_id1
	
	append using "$dataout\treatment"
	
	save "$dataout\firm_allmatches_long_`Y'_control.dta", replace
  
  
  
}



* create unique match_ids
forvalues Y=2006 (1) 2013 {
    use "$dataout\firm_allmatches_long_`Y'_control.dta", clear
		*** make sure each match has a unique id 
	sort match_id1 wp_crime 
	bysort match_id1: gen temp=_N
    sum match_id1, d
	gen max_match_id1=r(max)
	bysort match_id1: gen match_id1_temp=match_id1+max_match_id1 if temp==4 & (_n==2 | _n==4)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+1 if temp==6 & (_n==2 | _n==5)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+2 if temp==6 & (_n==3 | _n==6)
	
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+3 if temp==8 & (_n==2 | _n==6)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+4 if temp==8 & (_n==3 | _n==7)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+5 if temp==8 & (_n==4 | _n==8)
	
	
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+6 if temp==10 & (_n==2 | _n==7)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+7 if temp==10 & (_n==3 | _n==8)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+8 if temp==10 & (_n==4 | _n==9)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+9 if temp==10 & (_n==5 | _n==10)
	
	replace  match_id1= match_id1_temp if  match_id1_temp!=. 
	
	drop temp match_id1_temp
	bysort match_id1: gen temp=_N
	tab temp 
	drop temp max_match_id1
	save "$dataout\firm_allmatches_`Y'_control.dta",replace 

} 



// After we find the matches, link to the firm's data 5 years before and 5 years after the event
forvalues Y=2006 (1) 2013 {

	forvalues a= -5(1)5{
 
	use "$dataout\firm_allmatches_`Y'_control.dta", clear
	drop employee* earnings* share_female*
	
	
	gen time=`a'
	ereplace year_event=max(year_event)
	
	local k=`Y'+`a'

   
	merge m:1 sykstun using "${dataout}plant_char_`k'"
	drop if _merge==2
	

	save "$dataout\firm_allmatches_long_`Y'_`k'_control.dta", replace
    
	
	}

}



// Combine all years to form firm panel data 
use "$dataout\firm_allmatches_long_2013_2018_control.dta", clear

	forvalues i=-5 (1)4 {
	local a=2013+`i'
	append using "$dataout\firm_allmatches_long_2013_`a'_control.dta"
}



forvalues Y=2006 (1) 2012 {
	forvalues i=-5(1) 5{
		local a=`Y'+`i'
	append using "$dataout\firm_allmatches_long_`Y'_`a'_control.dta"
}
}


replace year=year_event+time




gen baseline_employee=employee if time==0 
sort sykstun year
bysort sykstun: replace baseline_employee=employee[_n-1] if employee==. & time==0
bysort sykstun year_event: ereplace baseline_employee=max(baseline_employee)



* generate base year total employees 
gen baseline_fe=female_employee if time==0 
sort sykstun year
bysort sykstun: replace baseline_fe=female_employee[_n-1] if female_employee==. & time==0
bysort sykstun year_event: ereplace baseline_fe=max(baseline_fe)



* generate base year total employees 
gen baseline_me=male_employee if time==0 
sort sykstun year
bysort sykstun: replace baseline_me=male_employee[_n-1] if male_employee==. & time==0
bysort sykstun year_event: ereplace baseline_me=max(baseline_me)

gegen match_id1=group(match_id1 year_event), replace 


save "${dataout}turnover_allyears_violentfirms_extra", replace 
gen step=1 

// merge with the previous firm sample 
append using "${dataout}turnover_allyears_violentfirms_old"
replace step=2 if step==. 

gegen match_id1=group(match_id1 step), replace 
 drop _merge
save "${dataout}turnover_allyears_violentfirms", replace 
